package com.flyee.viewer.ui.realty.solidhouseshow
{
	import com.flyee.controller.UIController;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.ClassLoader;
	import com.flyee.viewer.BaseWindowViewer;
	import com.flyee.viewer.ui.realty.common.ListWindow;
	
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.events.ProgressEvent;
	
	import mx.controls.ProgressBar;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	public class SolidHouseShowWindow extends UIComponent
	{
		
		private var myXML:XML = <data>
									<house>
										<name>A-01 3室1厅2卫</name>
										<solidurl>realty/solidHouse/house1.png</solidurl>
										<planeurl></planeurl>
									</house>
									<house>
										<name>A-01 3室1厅2卫</name>
										<solidurl>realty/solidHouse/house2.png</solidurl>
										<planeurl></planeurl>
									</house>
									<house>
										<name>A-01 3室1厅2卫</name>
										<solidurl>realty/solidHouse/house3.png</solidurl>
										<planeurl></planeurl>
									</house>
								</data>
		
		private var myBG:BaseWindowViewer = null;
		private var myList:ListWindow = null;
		private var myShow:ShowSolidHouse = null;
		private var dingzhiBtn:SimpleButton = null;
		private var showPlaneBtn:SimpleButton = null;
		private var myWidth:int = 650
		private var myHeight:int = 516
		private var progressWindow:ProgressBar = null;
		
		public static const NAME:String = "com.flyee.viewer.ui.realty.solidhouseshow.SolidHouseShowWindow"
		public function SolidHouseShowWindow(needList:Boolean = true,tagURL:String = null,listXML:XML = null)
		{
			//创建背景窗口
			createBG();
			
			//创建列表
			if(needList)
			{
				createList(listXML);
				//有列表的情况下 直接显示列表的第一个户型
				createShowHouse(myXML.house[0].solidurl);
			}
			else
			{
				//无列表的情况下 显示指定户型
				createShowHouse(tagURL);
			}
			
		}
		/**
		 *创建背景窗口 
		 * 
		 */		
		public function createBG():void
		{
			myBG = new BaseWindowViewer();
			myBG.width = myWidth;
			myBG.height = this.myHeight
			myBG.title = "户型展示";
			myBG.showCloseButton = true;

			myBG.addEventListener(CloseEvent.CLOSE,closeHandle);
			this.addChild(myBG);
		}
		/**
		 *创建列表 
		 * @param tagXML
		 * 
		 */		
		private function createList(tagXML:XML):void
		{
			if(tagXML != null)
			{
				this.myXML = tagXML;
			}
			var tempLength:Number = this.myXML.house.length();
			var tempArray:Array = new Array();
			for(var i:int = 0; i< tempLength;i++)
			{
				tempArray.push(this.myXML.house[i].name);
			}
			myList = new ListWindow();
			myList.createList(tempArray);
			//this.myBG.getContentPane().addChild(myList);
			//this.addChild(myList);
			this.myBG.addChild(myList);
			myList.addEventListener(ListWindow.LISTCLICKEVENT,changeShow);
		}
		/**
		 * 初始化显示内容
		 * @param url
		 * 
		 */		
		private function createShowHouse(url:String):void
		{
			myShow = new ShowSolidHouse(url,this.showHouseCreated);
			this.myBG.addChildAt(myShow,0);
		}
		/**
		 *显示内容创建完毕 
		 * 
		 */		
		public function showHouseCreated():void
		{
			if(this.dingzhiBtn == null)
			{
				createOther();
			}
		}
		/**
		 *创建窗口上的其他按钮 
		 * 
		 */		
		private function createOther():void
		{
			var tempLoader:ClassLoader = new ClassLoader();
			tempLoader.loadByURL("resource/realtyDepot.swf");
			tempLoader.addEventListener(ProgressEvent.PROGRESS,progressHandle);
			tempLoader.addEventListener(Event.COMPLETE,completeHandle);
			
		}
		private function progressHandle(e:ProgressEvent):void
		{
			if(this.progressWindow == null)
			{
				this.progressWindow = new ProgressBar();
				PopUpManager.addPopUp(this.progressWindow,this);
				PopUpManager.centerPopUp(this.progressWindow)
			}
			this.progressWindow.label = "加载留言板资源" + Math.round(e.bytesLoaded/e.bytesTotal*100)+"%";
		}
		private function completeHandle(e:Event):void
		{
			PopUpManager.removePopUp(this.progressWindow);
			this.progressWindow = null;
					
			var swfLoader:ClassLoader = e.target as ClassLoader;
			var tempClass:Class = swfLoader.getClassByName("dingzhi",swfLoader.loadinfo);
			this.dingzhiBtn = new tempClass() as SimpleButton;
			this.addChild(this.dingzhiBtn);
			this.dingzhiBtn.addEventListener(MouseEvent.CLICK,dingzhiHandle);
			this.dingzhiBtn.x = 510;
			this.dingzhiBtn.y = 430;
			
			tempClass = swfLoader.getClassByName("showPlane",swfLoader.loadinfo);
			this.showPlaneBtn = new tempClass() as SimpleButton;
			this.addChild(this.showPlaneBtn);
			this.showPlaneBtn.addEventListener(MouseEvent.CLICK,showPlaneHandle);
			this.showPlaneBtn.x = 265;
			this.showPlaneBtn.y = 450;
		}
		/**
		 *切换显示内容 
		 * @param e
		 * 
		 */		
		private function changeShow(e:NetStatusEvent):void
		{
			var tagURL:String = this.myXML.house[e.info].solidurl;
			this.myBG.removeChild(myShow);
			myShow.clear();
			myShow = null;
			myShow = new ShowSolidHouse(tagURL,showHouseCreated);
			this.myBG.addChildAt(myShow,0);
		}
		
		private function dingzhiHandle(e:MouseEvent):void
		{
		
		}
		private function showPlaneHandle(e:MouseEvent):void
		{
		
		}
		
		
		private function closeHandle(e:CloseEvent):void
		{
			var tempUI:UIController = ObjectCollection.beansContext.getObject("uiController") as UIController;
			tempUI.removedFromDesktop(this);
			this.clear()
		}
		public function clear():void
		{
			myBG.removeEventListener(CloseEvent.CLOSE,closeHandle);
			
			if(this.myList != null)
			{
				this.myBG.removeChild(this.myList);
				
				this.myList.removeEventListener(ListWindow.LISTCLICKEVENT,changeShow);
				this.myList = null;
			}
			if(this.dingzhiBtn != null)
			{
				this.removeChild(this.showPlaneBtn);
				this.removeChild(this.dingzhiBtn);
				this.dingzhiBtn.removeEventListener(MouseEvent.CLICK,dingzhiHandle);
				this.showPlaneBtn.removeEventListener(MouseEvent.CLICK,showPlaneHandle);
				this.dingzhiBtn = null;
				this.showPlaneBtn = null;
			}
			if(this.myShow != null)
			{
				this.myBG.removeChild(this.myShow);
				this.myShow = null;
			}
			myBG.removeEventListener(CloseEvent.CLOSE,closeHandle);
	
			myBG = null;
			this.myXML = null;
		}
	}
}